package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class CitygfNewsAnalyse implements AnalyseNews {

	//http://www.citygf.com/health/HL_016001/201110/t20111012_2336714.html
	private static final String[] regex={
		"http://www.citygf.com/.*/[0-9]*/t[0-9]*_[0-9]*.html",
	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
//		NewsMeta news=new ParseNewsUtils().parse(urlMeta);
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		if(doc.select("META[HTTP-EQUIV=Refresh]").size() > 0){
			news.setUpdateUrl(ParseState.ERR404.toString());
			return news;
		}
		title=doc.select("h2.title").text();
		if (StringUtils.isBlank(title))
			title=doc.select("#art-title").text();
		if (StringUtils.isBlank(title))
			title=doc.select("h1.tit").text();
		
		content=HtmlCleaner.getContentHtml(url ,doc.select("div.cont"));
		if (StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url ,doc.select("div#art-content"));
		}

		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来源：");

		date=JSoupUtils.matchDate(doc,"时间：");
		if (date==null){
			date=DateUtils.matchDate(doc.select("div.gbar").text());
			if (date==null){
				date=DateUtils.matchDate(doc.select("div.date").text());
			}
		}
		if(date==null)
			date =  DateUtils.matchDate(doc.select("div.from").text());

		news.setUrl(url);
		news.setTitle(title);
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		return news;
	}

	public static void main(String[] args) {
		String url="http://www.citygf.com/FD/FD_014008/201102/t20110223_1245008.html";
		CitygfNewsAnalyse test=new CitygfNewsAnalyse();
		System.out.println(test.isDetailPage(url));
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
